const JWT = require('jsonwebtoken');
module.exports = keys => {
  return async function jwt(ctx, next) {
    const token = ctx.request.header['x-token'];
    let decode;
    if (token) {
      try {
        // 解码token
        decode = JWT.verify(token, keys);
        await next();
      } catch (error) {
        ctx.status = 401;
        ctx.body = {
          code: 1,
          message: error.message,
        };
        return;
      }
    } else {
      ctx.status = 401;
      ctx.body = {
        code: 1,
        msg: '没有token',
      };
      return;
    }
  };
};
